// 封装请求的接口，返回promise
import ajax from "./ajax";
import axios from "axios";

import {message} from "antd";
// 重写请求路径
const BASE = ''

// 登录
export const reqLogin = (username, password)=>ajax(BASE+'/login',{username,password},'POST')

// 添加用户
export const reqAddUser = (user)=>ajax('/manage/user/add', {user},'POST')

// 更新用户
export const updateUser = (user)=>ajax('/manage/user/update', {user},'POST')

// 获取所有用户列表
export const getAllUserList = ()=>ajax('/manage/user/update')

// 删除用户
export const delUser = (userId)=>ajax('/manage/user/delete', {userId},'POST')

// 获取一级分类/二级分类列表
export const getCategoryList = (parentId)=>ajax(BASE+'/manage/category/list', {parentId})

// 添加分类
export const addCategory = (parentId,categoryName)=>ajax(BASE+BASE+'/manage/category/add',{parentId,categoryName},'POST')
// 更新品类名称
export const updateCategory =({categoryId, categoryName})=>ajax(BASE+BASE+'/manage/category/update',{categoryId,categoryName},'POST')
// 根据分类ID获取分类
export const getCategory =(categoryId)=>ajax(BASE+'/manage/category/info', {categoryId})
// 获取商品分页列表
export const getProductList = (pageNum,pageSize)=>ajax(BASE+'/manage/product/list',{pageNum,pageSize})

/**搜索产品分页列表(根据商品名称/商品描述)
 * searchType:搜索的类型，productName/productDesc
 * */
export const searchProducts = ({pageNum,pageSize,searchName,searchType})=>ajax(BASE+ '/manage/product/search',{
    pageNum,
    pageSize,
    [searchType]:searchName // 对象属性写法obj.name obj['name']
})


// 添加/修改商品
export const addOrUpdateProduct = (product) => ajax(BASE + '/manage/product/' + ( product._id?'update':'add'), product, 'POST')
// 对商品进行上架/下架处理



// jsonp请求的接口函数
export const reqWeather =  () => {

    return new Promise((resolve,reject)=>{
        axios.get('/api').then(response=>{
           resolve(response)
        }).catch(e=>{
           message.error('获取天气失败')
        })
    })

}





